package com.example.tetris.ui.GameBodyScreen

import android.view.MotionEvent.*
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.indication
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.input.pointer.pointerInteropFilter
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.example.tetris.ui.theme.Purple200
import com.example.tetris.ui.theme.Purple500
import kotlinx.coroutines.launch

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun GameButton(
    modifier: Modifier = Modifier,
    size: Dp,
    onClick: () -> Unit = {},
    autoInvokeWhenPressed: Boolean = false,
    content: @Composable (Modifier) -> Unit = {}
) {
    val backgroundShape = RoundedCornerShape(size / 2)
//    lateinit var ticker: ReceiveChannel<Unit>

    val coroutineScope = rememberCoroutineScope()

    val interactionSource = MutableInteractionSource()
    var pressedInteraction = remember {
        mutableStateOf<PressInteraction.Press?>(null)
    }



    Box(
        modifier = modifier
            .clickable { onClick() }
            .shadow(5.dp, backgroundShape)
            .size(size)
            .clip(backgroundShape)
            .background(
                Brush.verticalGradient(
                    colors = listOf(
                        Purple200, Purple500
                    ),
                    startY = 0f,
                    endY = 80f
                )
            )
            .indication(interactionSource, rememberRipple())
            .run {
                if (autoInvokeWhenPressed) {
                    pointerInteropFilter {
                        when (it.action) {
                            ACTION_DOWN -> {
                                coroutineScope.launch {
                                    pressedInteraction.value?.let {
                                        val interaction = PressInteraction.Cancel(it)
                                        interactionSource.emit(interaction)
                                        pressedInteraction.value = null
                                    }
                                    val press = PressInteraction.Press(
                                        androidx.compose.ui.geometry.Offset(
                                            50f,
                                            50f
                                        )
                                    )
                                    interactionSource.emit(press)
                                    pressedInteraction.value = press
                                }

//                                ticker = ticker(delayMillis = 60, initialDelayMillis = 300)
//                                coroutineScope.launch {
//                                    ticker.receiveAsFlow().collect {
//                                        onClick()
//                                    }
//                                }
                            }
                            ACTION_UP, ACTION_CANCEL -> {

                                coroutineScope.launch {
                                    pressedInteraction.value?.let {
                                        val interaction = PressInteraction.Cancel(it)
                                        interactionSource.emit(interaction)
                                        pressedInteraction.value = null
                                    }

//                                    ticker.cancel()
                                    if (it.action == ACTION_UP) onClick()
                                }
                            }
                            else -> {
                                if (it.action != ACTION_MOVE) {
//                                    ticker.cancel()
                                }
                                return@pointerInteropFilter false
                            }


                        }
                        true
                    }
                } else {
                    clickable {
                        onClick()
                    }
                }
            }
    ) {
        content(Modifier.align(Alignment.Center))
    }
}